/**************************************************************************************
*  std_c.c
*
*  This file contains implementation of replacement functions for some standard "C"
*  functions.
*
**************************************************************************************
*
* This file is covered by the GNU GPL with the following exception:
*   As a special exception, the copyright holders of this library give you permission
*   to link this library with independent modules to produce an executable, regardless
*   of the license terms of these independent modules, and to copy and distribute the
*   resulting executable under terms of your choice, provided that you also meet, for
*   each linked independent module, the terms and conditions of the license of that
*   module. An independent module is a module which is not derived from or based on
*   this library. If you modify this library, you may extend this exception to your
*   version of the library, but you are not obligated to do so. If you do not wish
*   to do so, delete this exception statement from your version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Copyright (c) 2000-2006 RTJ Computing Pty. Ltd. All rights reserved.
**************************************************************************************/
#include "std_c.h"

//-----------------------------------------------------------------------------
int strLen(char *s)
{
    int len = 0;
    
    while (*s++)
        len++;
    return len;
}
//-----------------------------------------------------------------------------
void strCpy(char *dest, char *text)
{
	int i = 0;

    while (*text)
		dest[i++] = *text++;
    dest[i] = 0;
}
//-----------------------------------------------------------------------------
void strCat(char *dest, char *text)
{
	int count, pos, i;

	pos = strLen((char *)dest);
	count = strLen((char *)text);
	for (i=0; i <= count; i++)
		dest[pos+i] = text[i];
}
//-----------------------------------------------------------------------------
// Returns: -1 if str1 is less than str2
//           0 if str1 equals to str2
//           1 if str1 is greater than str2
int strCmp(char *str1, char *str2)
{
    while (*str1 && *str2 && *str1 == *str2) {
        str1++;
        str2++;
    }
    if (*str1 == *str2)
        return 0;
    return *str1 < *str2 ? -1 : 1;
}
//-----------------------------------------------------------------------------
// Compares ignoring case
// Returns: -1 if str1 is less than str2
//           0 if str1 equals to str2
//           1 if str1 is greater than str2
int strCmpI(char *str1, char *str2)
{
    while (*str1 && *str2 && toUpperCase(*str1) == toUpperCase(*str2)) {
        str1++;
        str2++;
    }
    if (toUpperCase(*str1) == toUpperCase(*str2))
        return 0;
    return toUpperCase(*str1) < toUpperCase(*str2) ? -1 : 1;
}
//-----------------------------------------------------------------------------
char *strChr(char *str, char ch)
{
    while (*str) {
        if (*str == ch) 
            return str;
        str++;
    }

    return NULL;
}
//-----------------------------------------------------------------------------
char *strUpr(char *str)
{
    char *s = str;
    while (*s) {
        if (*s >= 'a' && *s <= 'z')
            *s -= 0x20;
        s++;
    }
    return str;
}
//-----------------------------------------------------------------------------
char *strLwr(char *str)
{
    char *s = str;
    while (*s) {
        if (*s >= 'A' && *s <= 'Z')
            *s += 0x20;
        s++;
    }
    return str;
}
//-----------------------------------------------------------------------------
char toHex(int val)
{
	val = (val & 0xF) + 0x30;
	if (val > 0x39)
		val += 7;
	return val;
}
//-----------------------------------------------------------------------------
char *toHex2(int val, char *dest)
{
	dest[0] = toHex(val >> 4);
	dest[1] = toHex(val);
	dest[2] = 0;
	return dest;
}
//-----------------------------------------------------------------------------
char *toHex4(int val, char *dest)
{
	char bf[4];
	dest[0] = 0;
	strCat(dest, toHex2(val >> 8, bf));
	strCat(dest, toHex2(val, bf));
	return dest;
}
//-----------------------------------------------------------------------------
char *toHex8(int val, char *dest)
{
	char bf[12];
	dest[0] = 0;
	strCat(dest, toHex4(val >> 16, bf));
	strCat(dest, toHex4(val, bf));
	return dest;
}
//-----------------------------------------------------------------------------
char *toDec(int val, char *dest)
{
	char bf[12];
	int i, cnt = 0;
	do {
		bf[cnt] = (val % 10) + '0';
		cnt++;
		val /= 10;
	} while (val > 0);
	for (i=0; cnt > 0; cnt--, i++)
		dest[i] = bf[cnt-1];
	dest[i] = 0;
	return dest;
}
//-----------------------------------------------------------------------------
int toUpperCase(int ch)
{
    if (ch >= 'a' && ch <= 'z')
        ch -= 0x20;
	return ch;
}
//-----------------------------------------------------------------------------
int toLowerCase(int ch)
{
    if (ch >= 'A' && ch <= 'Z')
        ch += 0x20;
	return ch;
}
//--------------------------------------------------------------------
void memSet(void *buf, char fill, int len)
{
    int i;

    if (buf == NULL)
        return;

    for (i=0; i < len; i++)
        ((byte *)buf)[i] = fill;
}
//--------------------------------------------------------------------
void memCpy(void *dest, void *src, int len)
{
    int i;

    if (src == NULL || dest == NULL)
        return;

    for (i=0; i < len; i++)
        ((byte *)dest)[i] = ((byte *)src)[i];
}
//-----------------------------------------------------------------------------
char *int2Str(char *buf, uint val, int digits, char fill, bool left_align)
{
    int i, rem;

    for (i=digits-1; i >= 0; i--) {
        rem = (val % 10);
        buf[i] = (rem == 0 && i != digits-1 && val == 0) ? fill : (char)(rem + 0x30);
        val /= 10;
    }
    buf[digits] = 0;
    if (left_align) {
        while (*buf == fill)
            buf++;
    }
    return buf;
}
//-----------------------------------------------------------------------------
int toDigit(uint ch)
{
	ch -= '0';
	if (ch > 9)
		ch -= 7;
	return ch;
}
//-----------------------------------------------------------------------------
int str2Int(char *buf, bool hex)
{
    int val = 0;
    char ch;

    if (!buf) 
        return 0;

    while (*buf) {
        ch = *buf++;
        if (hex) {
            if (ch >= 'a' && ch <= 'z')
                ch -= 0x20;
            if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
                val <<= 4;
                val += toDigit(ch);
            }
            else
                break;
        }
        else {
			if (ch >= '0' && ch <= '9') {
                val *= 10;
                val += (ch - '0');
            }
            else
                break;
        }
    }

    return val;
}
//-----------------------------------------------------------------------------

